﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServiceBus.EBSEngine;
using ServiceBus.EndPoint;
using ServiceBus.Contract;
using SAO;

namespace ServiceBusMonitor
{
    public partial class FmMonitor : Form
    {
        public FmMonitor()
        {
            InitializeComponent();

            FunctionServiceConnector.AddKnownType(typeof(SAO.Inventory));
            FunctionServiceConnector.AddKnownType(typeof(SAO.Inventory[]));

            BusEngine.RegisterNewService("Beijing Hub", "net.tcp://localhost:8886/Inventory");
            BusEngine.RegisterNewService("China Hub", "net.tcp://localhost:8887/Inventory");
            BusEngine.Disconnected += new EventHandler(BusEngine_Disconnected);
            BusEngine.HeartBeatMessage += new EventHandler(BusEngine_HeartBeatMessage);
            BusEngine.MessageDelivered += new EventHandler<ServiceBus.Contract.Message>(BusEngine_MessageDelivered);

            
        }
        private IStandardService BeijingFunction
        {
            get
            {
                return BusEngine.GetService("Beijing Hub").GetFunctionService<IStandardService>();
            }
        }
        private IStandardService ChinaFunction
        {
            get
            {
                return BusEngine.GetService("China Hub").GetFunctionService<IStandardService>();
            }
        }
        void BusEngine_MessageDelivered(object sender, ServiceBus.Contract.Message e)
        {
            if (e.ServiceName == "Beijing Hub")
            {
                int changednumber = Convert.ToInt32(e.Title);
                int productid = Convert.ToInt32(e.PrimaryID);
                Inventory inventory = this.BeijingFunction.Invoke("GetInventory", productid) as Inventory;
                switch (e.FunctionCode.ToUpper().Trim())
                {
                    case "NEWINVENTORY":
                        inventory.CurrentInventory *= 10;
                        this.ChinaFunction.Invoke("CreateNewProduct", inventory, e.Sender);
                        break;
                    case "OUTOFSTOCK":
                        this.ChinaFunction.Invoke("ChangeInventory",e.Sender,productid,changednumber);
                        break;
                    case "INVENTORYCHANGE":
                        if (inventory.CurrentInventory < 20)
                        {
                            if ((bool)this.ChinaFunction.Invoke("ChangeInventory", e.Sender, productid, inventory.CurrentInventory-50))
                            {
                                this.BeijingFunction.Invoke("ChangeInventory", e.Sender, productid, 50 - inventory.CurrentInventory);
                            }
                        }
                        //this.txtInfo.AppendText(e.Sender + " changed " + inventory.ProductName + " inventory to " +
                        //    inventory.CurrentInventory.ToString() + "\r\n");
                        break;
                    default:
                        break;
                }
            }
        }

        void BusEngine_HeartBeatMessage(object sender, EventArgs e)
        {
            this.Displayservices.First(s => s.Name == sender.ToString()).Status = "Connected";
            this.dgServices.DataSource = null;
            this.dgServices.DataSource = Displayservices;
        }

        void BusEngine_Disconnected(object sender, EventArgs e)
        {
            this.Displayservices.First(s => s.Name == sender.ToString()).Status = "DisConnected";
            this.dgServices.DataSource = null;
            this.dgServices.DataSource = Displayservices;
        }
        List<ServiceStruct> Displayservices = new List<ServiceStruct>();
        private void DisplayServices()
        {

            this.Displayservices.Clear();
            SAOServiceStructre<ISAOService>[] services = BusEngine.GetServiceList();
            foreach (SAOServiceStructre<ISAOService> s in services)
            {
                ServiceStruct service = new ServiceStruct() { Address = s.Address, Name = s.ServiceName, Status = "Not Connected" };
                Displayservices.Add(service);
            }
            this.dgServices.DataSource = Displayservices;
        }

        private void FmMonitor_Load(object sender, EventArgs e)
        {
            this.DisplayServices();
        }
    }
}
